// 引入axios
import axios from 'axios'
// 请求延时（毫秒数，如果请求话费超过了'timeout'的时间，请求将被中断）
axios.defaults.timeout = 100000

const api_name = 'http://localhost:8333/post/postInfo'

// 添加一个请求拦截器
axios.interceptors.request.use(function (config) {
    // 在请求发送之前，将 token 放入请求头中
    const token = localStorage.getItem('authorization');
    if (token) {
      config.headers.Authorization = `${token}`;
    }
    return config;
  }, function (error) {
    return Promise.reject(error);
  });

export default {

    //mypostPage
    myPostPage(page, limit, searchObj) {
        return axios.post(`${api_name}/` +"myPostPage/" + `${page}/${limit}`,{
            text : searchObj.text,
            type : searchObj.type,
        })
    },

    //帖子列表
    pageListWithLike(page, limit, searchObj) {
        return axios.post(`${api_name}/` +"pageWithLike/" + `${page}/${limit}`,{
            title : searchObj.title,
            text : searchObj.text,
            type : searchObj.type,
            userId : searchObj.userId,
        })
    },

    addPost(saveObj) {
        return axios.post(`${api_name}/` + "post", {
            title: saveObj.title,
            pic: saveObj.pic,
            text: saveObj.text,
            type: saveObj.type,
        })
    },

    like(id) {
        return axios.get(`${api_name}/` + "likes/" + `${id}`)
    },

    getAllPost(typeId) {
        return axios.get(`${api_name}/` + "getAllPost?type=" + `${typeId}`)
    },

    //帖子列表
    pageList(page, limit, searchObj) {
        return axios.post(`${api_name}/${page}/${limit}`,{
            title : searchObj.title,
            text : searchObj.text,
            type : searchObj.type,
            userId : searchObj.userId,
        })
    },
    //获取帖子
    getPostById(id) {
        return axios.get(`${api_name}/` + "get" + `/${id}`)
    },
    //修改帖子
    updatePost(editPost) {
        return axios.put(`${api_name}/` + "update",{
            id: editPost.id,
            title:editPost.title,
            text:editPost.text,
            pic:editPost.pic,
            userId:editPost.userId,
            type:editPost.type
        })
    },
    //删除某一个帖子
    remove(id) {
        return axios.delete(`${api_name}/` + "remove" + `/${id}`)
    },
}